package com.hnkywl.sts.dao.cwgl.pjdr;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.dao.cwgl.KmdmDao;
import com.hnkywl.sts.dao.cwgl.kmys.CykmdmysDao;
import com.hnkywl.sts.dao.cwgl.kmys.LjkmdmysDao;
import com.hnkywl.sts.entity.cwgl.Khzhsfmx;
import com.hnkywl.sts.entity.cwgl.VoucherItemData;
import com.hnkywl.sts.entity.cwgl.kmys.Cykmdmys;
import com.hnkywl.sts.entity.cwgl.kmys.Ljkmdmys;
import com.hnkywl.sts.entity.zjgl.Yhsx;
import com.hnkywl.sts.entity.zjgl.Yhsx;
import com.hnkywl.sts.entity.zjgl.Yhsxmx;
import com.hnkywl.sts.service.cwgl.KmdmService;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lenovo
 * Date: 13-6-3
 * Time: 下午3:11
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class YhsxdrDao extends SimpleHibernateTemplate<Yhsxmx,Long> {
    private KmdmService kmdmService;
    private CykmdmysDao cykmdmysDao;
    private LjkmdmysDao ljkmdmysDao;

    /**
     * 查询贴现承兑
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Yhsxmx> findYhsxdrList(Long Ids,String gsMc,Date jkrqStart,Date jkrqEnd,Integer yhsxZt,int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Yhsxmx> page = new Page<Yhsxmx>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (Ids != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" nbgs.id = ? ");
            param.add(Ids);
        }
        if (Ids == null && gsMc!=null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" nbgs.jc like ? ");
            param.add("%" + gsMc.trim() + "%");
        }
        if (jkrqStart != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" hkrq >= ?");
            param.add(jkrqStart);
        }
        if (jkrqEnd != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" hkrq <= ?");
            param.add(jkrqEnd);
        }
        if (yhsxZt != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            //status=0未导入
            //status=1导入成功
            //status=2导入失败
            //status=3删除成功
            //status=4删除失败
            condition.append("("+yhsxZt+"=0 and insertFlag=1) " +
                    "or ("+yhsxZt+"=1 and insertFlag=3 and deleteFlag!=2) " +
                    "or ("+yhsxZt+"=2 and insertFlag=2) " +
                    "or ("+yhsxZt+"=3 and deleteFlag=3 and insertFlag!=2) " +
                    "or ("+yhsxZt+"=4 and deleteFlag=2) ");
        }
        //==============记录数量===============
        hql = "select count(*) from Yhsxmx  where 1=1 "  + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        hql = hql + " order by hkrq desc";
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Yhsxmx where 1=1 "  + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        hql = hql + " order by hkrq desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public List<Khzhsfmx> getKhhzfmx(Long YhsxId){
        String   hql = " from Khzhsfmx where Yhsx.id = ? ";
        return  find(hql,YhsxId);
    }


    /**
     * 通过银行授信回凭证部分
     * @param
     * @return
     */
    public List<VoucherItemData> getVoucher(Yhsxmx yhsxmx) {
        List<VoucherItemData> voucherItemDatas = new ArrayList<VoucherItemData>();
        Double je1 = 0d;
        Double je2 = 0d;
        Double je3 = 0d;
        Double je4 = 0d;
        Double je5 = 0d;
        //银行存款 也就是收款没有做
        if(yhsxmx.getYhsxsflx().equals("sk")){
            Cykmdmys   cykmdmys5 =this.cykmdmysDao.getCommentSubject(yhsxmx.getYhsx().getNbgs().getId(), "2901");
            String ccname5 =cykmdmys5.getJqcwkmmc();
            String ccode5 =cykmdmys5.getKmdm();
            String ccode1 = yhsxmx.getYhsx().getKhh().getKmdm();
            String ccname1 ="银行存款/"+yhsxmx.getYhsx().getKhh().getQc();
            je1 = Double.parseDouble(yhsxmx.getYhsx().getJkje().toString());
            je5 = Double.parseDouble(yhsxmx.getYhsx().getJkje().toString());
                if(je1 != 0){
                    VoucherItemData voucherItemData1 = new VoucherItemData();
                    voucherItemData1.setZy("借"+yhsxmx.getYhsx().getKhh().getQc()+"银行授信");
                    voucherItemData1.setKmdm(ccode1);
                    voucherItemData1.setKmmc(ccname1);
                    voucherItemData1.setJf(je1);
                    if(yhsxmx.getYhsx().getKhh().getDrzh().equals("财务")){
                        voucherItemData1.setKmmc("财务公司存款/"+yhsxmx.getYhsx().getKhh().getQc());
                        voucherItemData1.setCustomerName(yhsxmx.getYhsx().getKhh().getQc());
                        voucherItemData1.setCustomerCode(yhsxmx.getYhsx().getKhh().getKhfzhsdm());
                    }
                    voucherItemDatas.add(voucherItemData1);
                }
                if(je5 !=0){
                    VoucherItemData voucherItemData5 = new VoucherItemData();
                    voucherItemData5.setZy("借"+yhsxmx.getYhsx().getKhh().getQc()+"银行授信");
                    voucherItemData5.setKmdm(ccode5);
                    voucherItemData5.setKmmc(ccname5);
                    voucherItemData5.setDf(je5);
                    String fzhs5 =getFzhsByCode(yhsxmx.getYhsx().getNbgs().getId(),ccode5.toString());
                    voucherItemData5 = getFzhsMxByFzsh(fzhs5,voucherItemData5,yhsxmx);
                    voucherItemDatas.add(voucherItemData5);
                }
        }else{
        // 付款
            //1.还款加上还利息
            if(yhsxmx.getHkje() !=null && Double.parseDouble(yhsxmx.getHkje().toString()) != 0 && yhsxmx.getHxje() !=null && Double.parseDouble(yhsxmx.getHxje().toString()) != 0){
                je1 = Double.parseDouble(yhsxmx.getHkje().toString());
                je2 = yhsxmx.getHxje();
                je3 = yhsxmx.getHxje();
                je4 = yhsxmx.getHxje();
                je5 = Double.parseDouble(yhsxmx.getHkje().toString())+yhsxmx.getHxje();
            }
            //还款
            if(yhsxmx.getHkje() !=null && Double.parseDouble(yhsxmx.getHkje().toString()) != 0 && yhsxmx.getHxje() ==null || yhsxmx.getHxje() == 0){
                je1 = Double.parseDouble(yhsxmx.getHkje().toString());
                je5 = Double.parseDouble(yhsxmx.getHkje().toString());
            }
            //还息
            if(yhsxmx.getHxje() !=null && yhsxmx.getHxje() != 0 && yhsxmx.getHkje() ==null || Double.parseDouble(yhsxmx.getHkje().toString()) == 0){
                je2 = yhsxmx.getHxje();
                je3 = yhsxmx.getHxje();
            }
            Cykmdmys   cykmdmys1 =this.cykmdmysDao.getCommentSubject(yhsxmx.getYhsx().getNbgs().getId(), "2901");
            String ccname1 =cykmdmys1.getJqcwkmmc();
            String ccode1 =cykmdmys1.getKmdm();
            Cykmdmys   cykmdmys2 =this.cykmdmysDao.getCommentSubject(yhsxmx.getYhsx().getNbgs().getId(), "3300");
            String ccname2 =cykmdmys2.getJqcwkmmc();
            String ccode2 =cykmdmys2.getKmdm();
            Cykmdmys   cykmdmys3 =this.cykmdmysDao.getCommentSubject(yhsxmx.getYhsx().getNbgs().getId(), "2700");
            String ccname3 =cykmdmys3.getJqcwkmmc();
            String ccode3 =cykmdmys3.getKmdm();
            Cykmdmys   cykmdmys4 =this.cykmdmysDao.getCommentSubject(yhsxmx.getYhsx().getNbgs().getId(), "2700");
            String ccname4 =cykmdmys4.getJqcwkmmc();
            String ccode4 =cykmdmys4.getKmdm();
            String ccode5 = yhsxmx.getYhsx().getKhh().getKmdm();
            String ccname5 ="银行存款/"+yhsxmx.getYhsx().getKhh().getQc();
                if(je1 != 0){
                    VoucherItemData voucherItemData1 = new VoucherItemData();
                    voucherItemData1.setZy("还"+yhsxmx.getYhsx().getKhh().getQc()+"银行授信");
                    voucherItemData1.setKmdm(ccode1);
                    voucherItemData1.setKmmc(ccname1);
                    voucherItemData1.setJf(je1);
                    String fzhs1 =getFzhsByCode(yhsxmx.getYhsx().getNbgs().getId(),ccode1.toString());
                    voucherItemData1 = getFzhsMxByFzsh(fzhs1,voucherItemData1,yhsxmx);
                    voucherItemDatas.add(voucherItemData1);
                }
                if(je4 !=0){
                    VoucherItemData voucherItemData4 = new VoucherItemData();
                    voucherItemData4.setZy("还"+yhsxmx.getYhsx().getKhh().getQc()+"银行授信");
                    voucherItemData4.setKmdm(ccode4);
                    voucherItemData4.setKmmc(ccname4);
                    voucherItemData4.setJf(je4);
                    String fzhs4 =getFzhsByCode(yhsxmx.getYhsx().getNbgs().getId(),ccode1.toString());
                    voucherItemData4 = getFzhsMxByFzsh(fzhs4,voucherItemData4,yhsxmx);
                    voucherItemDatas.add(voucherItemData4);
                }  if(je5 !=0){
                    VoucherItemData voucherItemData5 = new VoucherItemData();
                    voucherItemData5.setZy("还"+yhsxmx.getYhsx().getKhh().getQc()+"银行授信");
                    if(yhsxmx.getYhsx().getKhh().getDrzh().equals("财务")){
                        voucherItemData5.setKmmc("财务公司存款/"+yhsxmx.getYhsx().getKhh().getQc());
                        voucherItemData5.setCustomerName(yhsxmx.getYhsx().getKhh().getQc());
                        voucherItemData5.setCustomerCode(yhsxmx.getYhsx().getKhh().getKhfzhsdm());
                    }
                    voucherItemData5.setKmdm(ccode5);
                    voucherItemData5.setKmmc(ccname5);
                    voucherItemData5.setDf(je5);
                    voucherItemDatas.add(voucherItemData5);
                }


        }

        return voucherItemDatas;
    }
//
    public String getFzhsByCode(Long gsId,String code){
        return this.kmdmService.findKmdmDetailByGs(gsId,code).getFzhslx();
    }

    public VoucherItemData getFzhsMxByFzsh(String fzhs,VoucherItemData voucherItemData,Yhsxmx yhsxmx){
        fzhs = fzhs.replaceAll(" ","");
        String[] fzhss = fzhs.split("；");
        if(fzhss.length>0){
            for(String f :fzhss){
                if(f.equals("供应商往来")){
                    if(yhsxmx.getYhsx().getSfYhdk()==false && yhsxmx.getYhsx().getDkf()==null){
                        Ljkmdmys ljkmdmys = this.ljkmdmysDao.findKmdmByGsjgId(yhsxmx.getYhsx().getDkf().getId());
                        voucherItemData.setSupplierCode(ljkmdmys.getGysfzhsdm());
                        voucherItemData.setSupplierName(yhsxmx.getYhsx().getDkf().getQc());
                    }else{
                        voucherItemData.setSupplierCode(yhsxmx.getYhsx().getKhh().getGysfzhsdm());
                        voucherItemData.setSupplierName(yhsxmx.getYhsx().getKhh().getJc());
                    }
                }
            }
        }
        return voucherItemData;
    }

    public Integer praseLongToIntegr(Long l){
        return Integer.parseInt(l.toString());
    }
    @Autowired
    public void setKmdmService(KmdmService kmdmService) {
        this.kmdmService = kmdmService;
    }
    @Autowired
    public void setCykmdmysDao(CykmdmysDao cykmdmysDao) {
        this.cykmdmysDao = cykmdmysDao;
    }
}
